package com.zoke.daycarpool.ui;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;

import com.zoke.daycarpool.R;
import com.zoke.daycarpool.base.BaseUi;
import com.zoke.daycarpool.domain.UserBeen;
import com.zoke.daycarpool.domain.UserInstense;
import com.zoke.support.view.ZokeToast;

/**
 * 编辑名字
 * 
 * @author jack
 * 
 */
public class EditPhone extends BaseUi {
	private EditText mPhoneEt;

	private UserBeen mUser;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_editname);
		mTitleManager.setTitle("电话").setLeftIcon(R.drawable.title_back);
		mTitleManager.getTitleRight().setTextColor(
				getResources().getColor(R.color._main));
		mTitleManager.setRightText("完成");
		mEnableSwipeRight = true;
		mUser = UserInstense.getInstense(getApplicationContext()).getUser();
		// 显示键盘
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);// 显示键盘
		findView();
		doEvent();
		if (mUser == null)
			return;
		mPhoneEt.setText(mUser.getPhone());
		mPhoneEt.setInputType(InputType.TYPE_CLASS_PHONE);
	}

	@Override
	protected void findView() {
		mPhoneEt = (EditText) findViewById(R.id._edit);
	}

	@Override
	protected void doEvent() {
	}

	public static boolean isCellphone(String str) {
		Pattern pattern = Pattern.compile("1[0-9]{10}");
		Matcher matcher = pattern.matcher(str);
		if (matcher.matches()) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public void onTitleClickRight(View view) {
		super.onTitleClickRight(view);
		String phone = mPhoneEt.getText().toString();
		if (TextUtils.isEmpty(phone.trim())) {
			mPhoneEt.setError("电话号码不能为空");
			return;
		}

		if (!isCellphone(phone.trim())) {
			mPhoneEt.setError("必须为正确的电话号码");
			return;
		}

		Intent intent = new Intent();
		intent.putExtra("phone", phone);
		setResult(PersonEdit.RESULTCODE_PHONE, intent);
		this.onBackPressed();
	}
}
